export default {
  state: {
    keepAliveVue: false,
    keepAliveVue2: false,
    isCollapse: false,
    role: 0,
    pdfUrl: '',
    our_addr: {
      my_addr: ''
    },
    tabsList: [
      // {
      //   path: '/',
      //   name: 'home',
      //   label: '首页',
      //   icon: 's-home',
      //   url: 'Home/Home'
      // }
    ],
    userInfo: {},
    menu: [],
    userImage: ''
  },
  mutations: {
    // 刷新页面
    reOnload(state) {
      state.keepAliveVue = !state.keepAliveVue
    },
    // 退出登录
    reOnload2(state) {
      state.keepAliveVue2 = !state.keepAliveVue2
    },
    // 修改菜单展开收起的方法
    collapseMenu(state) {
      state.isCollapse = !state.isCollapse
    },
    // 获取地址
    selectLocation(state, val) {
      state.our_addr = val
    },
    // 修改登录角色
    updateRole(state, item) {
      state.role = item
      // console.log(state.role)
    },
    // 更新面包屑数据
    selectMenu(state, val) {
      // console.log(val, 'val')
      // 判断添加的数据是否为首页
      if (val.name !== 'home') {
        const index = state.tabsList.findIndex(item => item.name === val.name)
        // 如果不存在
        if (index === -1) {
          state.tabsList.push(val)
        }
      }
    },
    // 删除指定的tag数据
    closeTag(state, item) {
      // console.log(item, 'item')
      const index = state.tabsList.findIndex(val => val.name === item.name)
      state.tabsList.splice(index, 1)
    },
    // 清空tabList表
    deleteTagAll(state) {
      state.tabsList.splice(0)
    },
    setMenu(state, val) {
      state.menu = val
      window.localStorage.setItem('menu', JSON.stringify(val))
    },
    addMenu(state, router) {
      if (!window.localStorage.getItem('menu')) return
      const menu = JSON.parse(window.localStorage.getItem('menu'))
      state.menu = menu
      const menuArray = []
      menu.forEach(item => {
        if (item.children) {
          item.children = item.children.map(item => {
            item.component = () => import(`../page/${item.url}`)
            return item
          })
          menuArray.push(...item.children)
        } else {
          item.component = () => import(`../page/${item.url}`)
          menuArray.push(item)
        }
      })
      // console.log(menuArray)
      menuArray.forEach(item => {
        router.addRoute('main', item)
      })
    },
    // 保存上传头像
    saveImage(state, val) {
      state.userImage = val
    },
    // 获取用户信息
    getUserInfo(state, val) {
      state.userInfo = val
      window.localStorage.setItem('userInfo', JSON.stringify(val))
    },
    // 保存我们刚上传的文件路径
    savePdfUrl(state, val) {
      state.pdfUrl = val
    }
  }
}
